package com.iamk.util;

import java.io.FileOutputStream;
import java.io.PrintStream;

import weka.classifiers.Classifier;
import weka.classifiers.functions.SMO;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Normalize;
import weka.filters.unsupervised.attribute.ReplaceMissingValues;

public class CompClassifier {
	public static FileOutputStream Output;
	public static PrintStream file;

	public static void main(String[] args) throws Exception {
		// load training data
		weka.core.Instances training_data = new weka.core.Instances(new java.io.FileReader("C:/Users/sev_user/Desktop/data-mcr/data-mcr/Feature/features-taumura-dcd-7.arff"));

		// load test data
		weka.core.Instances test_data = new weka.core.Instances(new java.io.FileReader("C:/Users/sev_user/Desktop/data-mcr/data-mcr/Feature/features-taumura-dcd-7-test.arff"));

		// Clean up training data
		ReplaceMissingValues replace = new ReplaceMissingValues();
		replace.setInputFormat(training_data);
		Instances training_data_filter1 = Filter.useFilter(training_data, replace);

		// Normalize training data
		Normalize norm = new Normalize();
		norm.setInputFormat(training_data_filter1);
		Instances processed_training_data = Filter.useFilter(training_data_filter1, norm);

		// Set class attribute for pre-processed training data
		processed_training_data.setClassIndex(processed_training_data.numAttributes() - 1);

		// output to file
		Output = new FileOutputStream("C:/Users/sev_user/Desktop/data-mcr/data-mcr/Feature/test.txt");
		file = new PrintStream(Output);

		// build classifier
//		Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/nBayes.model"); // Load Classifier from saved model
		SMO tree = new SMO();
		tree.buildClassifier(processed_training_data);

		// Clean up test data
		replace.setInputFormat(test_data);
		Instances test_data_filter1 = Filter.useFilter(test_data, replace);

		// Normalize test data
		norm.setInputFormat(training_data_filter1);
		Instances processed_test_data = Filter.useFilter(test_data_filter1,	norm);

		// Set class attribute for pre-processed training data
		processed_test_data.setClassIndex(processed_test_data.numAttributes() - 1);

		// int num_correct=0;
		for (int i = 0; i < processed_test_data.numInstances(); i++) {
			weka.core.Instance currentInst = processed_test_data.instance(i);
			int predictedClass = (int) tree.classifyInstance(currentInst);
			System.out.println(predictedClass+1);
			file.println("O" + predictedClass);
		}

	}
}
